package com.qf.controller.admin;

import com.qf.entity.Goods;
import com.qf.entity.User;
import com.qf.service.GoodsService;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.UUID;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package ${PACKAGE_NAME}
 * @description: ${TODO}
 * @date 2022/6/27 16:10
 */
@WebServlet(name = "AddGoodsServlet",urlPatterns = "/addGoods")
@MultipartConfig(maxFileSize = 1024*1024*5,maxRequestSize = 1024*1024*20)
public class AddGoodsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       request.setCharacterEncoding("utf-8");

        //1判断是否登录 admin
        User admin= (User) request.getSession().getAttribute("admin");
        if(admin==null){
            response.sendRedirect(request.getContextPath()+"/admin/login.jsp");
            return;
        }
        //2获取数据
        String name = request.getParameter("name");
        String typeid = request.getParameter("typeid");
        String pubdate = request.getParameter("pubdate");
        String price = request.getParameter("price");
        String star = request.getParameter("star");
        String intro = request.getParameter("intro");
        Part picture = request.getPart("picture");
        //3校验
        //3.1非空判断
        if(StringUtils.isEmpty(name)){
            response.sendRedirect(request.getContextPath()+"/admin/addGoods.jsp");
            return;
        }
        if(StringUtils.isEmpty(typeid)){
            response.sendRedirect(request.getContextPath()+"/admin/addGoods.jsp");
            return;
        }
        if(StringUtils.isEmpty(pubdate)){
            response.sendRedirect(request.getContextPath()+"/admin/addGoods.jsp");
            return;
        }
        if(StringUtils.isEmpty(price)){
            response.sendRedirect(request.getContextPath()+"/admin/addGoods.jsp");
            return;
        }
        if(StringUtils.isEmpty(star)){
            response.sendRedirect(request.getContextPath()+"/admin/addGoods.jsp");
            return;
        }
        if(StringUtils.isEmpty(intro)){
            response.sendRedirect(request.getContextPath()+"/admin/addGoods.jsp");
            return;
        }
        String basepath=this.getServletContext().getRealPath("WEB-INF"+ File.separator+"images");
        File dir=new File(basepath);
        if(!dir.exists()){
            dir.mkdirs();
        }
        //3.2处理图片
        String submittedFileName = picture.getSubmittedFileName();
        String filename=null;
        if(!submittedFileName.equals("")){
            //提交了图片
            //生成新的文件名
            filename = UUID.randomUUID().toString().replace("-", "").substring(16);
            //获取后缀
            String ext=submittedFileName.substring(submittedFileName.lastIndexOf(".")+1);
            filename=filename+"."+ext;
            //保存文件
            picture.write(basepath+ File.separator+filename);
        }
        //4 创建业务对象
        GoodsService goodsService=new GoodsServiceImpl();
        //5调用方法
        response.setContentType("text/html;charset=utf-8");
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Goods goods=new Goods(0, name, sdf.parse(pubdate), filename, new BigDecimal(price), Integer.parseInt(star),intro,Integer.parseInt(typeid));
            goodsService.add(goods);
            response.getWriter().write("<script>alert('添加商品成功');window.location='admin.jsp'</script>");
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().write("<script>alert('添加商品失败,"+e.getMessage()+"');window.location='admin.jsp'</script>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
